package boogui.movie;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import boogui.member.S_InitIcon;
import java.awt.GridBagLayout;
import javax.swing.JTextPane;
import java.awt.GridBagConstraints;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.border.EtchedBorder;
import java.awt.Rectangle;
import java.awt.Component;

public class S_MovieMain extends JFrame {

	private static String infoDirector = "";
	private static MovieSelect mselect = null;
	private static MovieVO m = null;
	private static S_MovieMain frame;
	private static MovieAreaVO ma = null;
	private static MovieService ms = null;
	private JPanel contentPane;
	private JTable movarea;
	private JTable movcinema;
	private JTable calendar;
	private JTable movtheater;
	private JPanel movresv;
	private JTable movname;

	private JTable table;
	protected DefaultTableModel model2;
	private JScrollPane scrollPane;
	private static JScrollPane sPmovarea;
	private static JScrollPane sPmovcinema;
	private JPanel movpanel;
	private JLabel isPlaylbl;
	private JLabel Posterlbl;
	private JLabel Synoplnl;
	private static JPanel movieinfopanel;
	private JLabel label;
	private JLabel movieinfo1;

	private static String movinfo;
	private static String synop;
	private JTextArea movsynop1;
	private JPanel panel;
	private JLabel lblNewLabel;
	private JPanel panel_1;
	private JLabel label_1;
	private static String isplay;

	public S_MovieMain() {

		ms = new MovieService();
		sPmovarea = new JScrollPane(); // 스크롤페인 지역
		sPmovcinema = new JScrollPane(); // 스크롤 페인 영화관
		mselect = new MovieSelect();
		ma = new MovieAreaVO();
		m = new MovieVO();
		movieinfo1 = new JLabel();
		movieinfopanel = new JPanel();
		movsynop1 = new JTextArea();

		setIconImage(S_InitIcon.initIcon());
		setTitle("부귀영화");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(1024, 768);
		setLocationRelativeTo(null);
		setResizable(false);
		contentPane = new JPanel();
		S_MovieMenu movieMenu = new S_MovieMenu(frame);
		setJMenuBar(movieMenu);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		movresv = new JPanel();
		movresv.setBorder(new EmptyBorder(1, 10, 10, 10));
		FlowLayout fl_movresv = (FlowLayout) movresv.getLayout();
		fl_movresv.setAlignment(FlowLayout.LEFT);
		movresv.setPreferredSize(new Dimension(10, 300));
		contentPane.add(movresv, BorderLayout.SOUTH);

		movpanel = new JPanel();

		movpanel.setBorder(new EmptyBorder(30, 10, 10, 10));
		movpanel.setPreferredSize(new Dimension(10, 400));
		contentPane.add(movpanel, BorderLayout.NORTH);
		movpanel.setLayout(new BorderLayout(0, 0));

		Posterlbl = new JLabel("");
		Posterlbl.setIcon(new ImageIcon(
				"C:\\Users\\PC-02\\Desktop\\poster\\1s1.jpg"));
		Posterlbl.setHorizontalAlignment(SwingConstants.CENTER);
		Posterlbl.setPreferredSize(new Dimension(230, 300));
		movpanel.add(Posterlbl, BorderLayout.WEST);

		movpanel.add(movieinfopanel, BorderLayout.CENTER);
		movieinfopanel.setLayout(new BorderLayout(0, 0));

		movietitle();

		setVisible(true);

	}

	private void movietitle() {

		JScrollPane sPmovname = new JScrollPane(); // 스크롤페인 영화이름

		sPmovname.setFont(new Font("굴림", Font.PLAIN, 18));
		sPmovname
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		sPmovname
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		sPmovname.setPreferredSize(new Dimension(195, 200));
		movresv.add(sPmovname);

		DefaultTableModel model = new DefaultTableModel() {

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;// 열수정 불가 } };
			}
		};
		String title[] = { "<html><font color='red' size='6' face='맑은 고딕'><i>상영중인 영화</i></font></html>" };
		model.setColumnIdentifiers(title);

		for (MovieVO m : mselect.clickMovie()) {
			model.addRow(new String[] { m.getMovtitle() });
		}

		movname = new JTable(model);
		movname.setBackground(Color.WHITE);
		movname.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				if (e.getClickCount() == 1) {
					JTable table = (JTable) e.getSource();
					int row = table.getSelectedRow(); // 선택행
					int col = table.getSelectedColumn();// 선택열

					String val = (String) table.getValueAt(row, col);

					m.setMovtitle(val);

					moviearea(mselect.clickArea(m));

					movieinfo(m);
					movsynop(m);
					movisplay(m);

					setVisible(true);
				}
			}

		});
		sPmovname.setViewportView(movname);
		moviearea(new ArrayList<MovieAreaVO>());
	}

	private void movieinfo(MovieVO m) {

		ms.searchmovie(m);
		// System.out.println(m.getMovactor1());

		String a = m.getMovgenre2();
		if (a == null) {
			a = "";
		} else {
			m.getMovgenre2();
		}

		movinfo = "<html>감 독&nbsp;&nbsp;&nbsp;&nbsp;" + m.getMovdirector()
				+ "<br/><br/>출 연&nbsp;&nbsp;&nbsp;&nbsp;" + m.getMovactor1()
				+ "&nbsp;" + m.getMovactor2() + "&nbsp;" + m.getMovactor3()
				+ "<br/><br/>등 급&nbsp;&nbsp;&nbsp;&nbsp;" + m.getMovrating()
				+ "<br/><br/>개봉일&nbsp;&nbsp;&nbsp;" + m.getMovplaydate()
				+ "<br/><br/>장 르&nbsp;&nbsp;&nbsp;&nbsp;" + m.getMovgenre1()
				+ "&nbsp;" + a + "<br/><br/>상영시간&nbsp;" + m.getMovruntime()
				+ "분</html>";
		// JScrollPane sPmovname = new JScrollPane(); // 스크롤페인 영화이름
		movieinfopanel.remove(movieinfo1);
		movieinfo1 = new JLabel(movinfo);
		movieinfo1.setBorder(new EmptyBorder(10, 30, 0, 0));
		movieinfo1.setVerticalAlignment(SwingConstants.TOP);
		movieinfo1.setPreferredSize(new Dimension(375, 260));
		movieinfo1.setHorizontalAlignment(SwingConstants.LEFT);
		movieinfo1.setFont(new Font("굴림", Font.PLAIN, 15));
		movieinfopanel.add(movieinfo1, BorderLayout.WEST);

	}

	private void movisplay(MovieVO m) {
		m.setMovisplay("");
		ms.searchmovie(m);
		switch (m.getMovisplay()) {
		case "N":
			isplay = "상영 예정 작";
			break;
		case "Y":
			isplay = "현재 상영 중 ";
			break;
		default:
		}

		isPlaylbl = new JLabel(isplay);
		movieinfopanel.remove(isPlaylbl);
		isPlaylbl.setBackground(new Color(0, 153, 153));
		isPlaylbl.setFont(new Font("굴림", Font.PLAIN, 20));
		isPlaylbl.setHorizontalAlignment(SwingConstants.LEFT);
		movpanel.add(isPlaylbl, BorderLayout.NORTH);

	}

	private void movsynop(MovieVO m) {

		ms.searchmovie(m);

		synop = m.getMovsynop();

		movieinfopanel.remove(movsynop1);

		panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		movieinfopanel.add(panel, BorderLayout.NORTH);

		lblNewLabel = new JLabel("영화 제목\r\n");
		lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 25));
		lblNewLabel.setBackground(new Color(243, 243, 243));
		panel.add(lblNewLabel);

		panel_1 = new JPanel();
		movieinfopanel.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		movsynop1 = new JTextArea(synop);
		panel_1.add(movsynop1);
		movsynop1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		movsynop1.setBackground(new Color(238, 238, 238));
		movsynop1.setLineWrap(true);
		movsynop1.setPreferredSize(new Dimension(200, 200));

		label_1 = new JLabel("줄거리");
		label_1.setFont(new Font("굴림", Font.PLAIN, 15));
		panel_1.add(label_1, BorderLayout.NORTH);

	}

	// ////// 무비이름
	private void moviearea(List<MovieAreaVO> areas) {

		// 영화명을 클릭하기전에는 enable false 로 정의
		// sPmovarea = new JScrollPane(); // 스크롤페인 지역
		sPmovarea.setEnabled(false);
		sPmovarea.setFont(new Font("굴림", Font.PLAIN, 18));
		sPmovarea
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		sPmovarea
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		sPmovarea.setPreferredSize(new Dimension(195, 200));
		movresv.add(sPmovarea);

		DefaultTableModel model2 = new DefaultTableModel() {

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;// 열수정 불가 } };
			}
		};
		String title2[] = { "<html><font color='red' size='6' face='맑은 고딕'><i>상영중인 지역</i></font></html>" };
		model2.setColumnIdentifiers(title2);

		for (MovieAreaVO a : areas) {
			model2.addRow(new String[] { a.getLocation() });
		}
		movarea = new JTable(model2);
		movarea.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				JTable table = (JTable) e.getSource();
				int row = table.getSelectedRow(); // 선택행
				int col = table.getSelectedColumn();// 선택열

				String val = (String) table.getValueAt(row, col);

				ma.setLocation(val);

				moviecinema(mselect.clickCinema(m, ma));

			}
		});

		sPmovarea.setViewportView(movarea);
		moviecinema(new ArrayList<MovieAreaVO>());
		setVisible(true);
	}

	private void moviecinema(List<MovieAreaVO> areas) {

		// 영화명을 클릭하기전에는 enable false 로 정의
		// sPmovarea = new JScrollPane(); // 스크롤페인 지역
		sPmovcinema.setEnabled(false);

		sPmovcinema.setFont(new Font("굴림", Font.PLAIN, 18));
		sPmovcinema
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		sPmovcinema
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		sPmovcinema.setPreferredSize(new Dimension(195, 200));
		movresv.add(sPmovcinema);

		DefaultTableModel model3 = new DefaultTableModel() {

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;// 열수정 불가 } };
			}
		};
		String title3[] = {
				"<html><font color='red' size='4' face='맑은 고딕'><i>상영중인</i></font></html>",
				"<html><font color='red' size='4' face='맑은 고딕'><i>영화관</i></font></html>" };
		model3.setColumnIdentifiers(title3);
		for (MovieAreaVO a : areas) {
			model3.addRow(new String[] { a.getCinename(), a.getTheater() });

		}

		movcinema = new JTable(model3);
		sPmovcinema.setViewportView(movcinema);
		setVisible(true);
	}

	private void ShowAllJTable() {

		// /영화관

		JScrollPane sPcalendar = new JScrollPane(); // 스크롤페인 캘린더
		sPcalendar.setFont(new Font("굴림", Font.PLAIN, 18));
		sPcalendar
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		sPcalendar
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		sPcalendar.setPreferredSize(new Dimension(195, 200));
		movresv.add(sPcalendar);

		DefaultTableModel model4 = new DefaultTableModel() {

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;// 열수정 불가 } };
			}
		};
		String title4[] = { "<html><font color='red' size='6' face='맑은 고딕'><i>달력</i></font></html>" };
		model4.setColumnIdentifiers(title4);

		model4.addRow(new String[] { "달력" });

		calendar = new JTable(model4);
		sPcalendar.setViewportView(calendar);
		// /달력

		JScrollPane sPtheater = new JScrollPane(); // 스크롤페인 캘린더
		sPtheater.setPreferredSize(new Dimension(195, 200));
		sPtheater.setFont(new Font("굴림", Font.PLAIN, 18));
		sPtheater
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		sPtheater
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		sPtheater.setPreferredSize(new Dimension(195, 200));
		movresv.add(sPtheater);

		DefaultTableModel model5 = new DefaultTableModel() {

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;// 열수정 불가 } };
			}
		};
		String title5[] = { "<html><font color='red' size='6' face='맑은 고딕'><i>theater</i></font></html>" };
		model5.setColumnIdentifiers(title5);

		model5.addRow(new String[] { "theater" });

		movtheater = new JTable(model5);
		sPtheater.setViewportView(movtheater);
		// theater

	}

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new S_MovieMain();
					UIManager
							.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

					frame.setIconImage(S_InitIcon.initIcon());

					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

}